/*
 * @描述信息: 保存视频到手机本地相册
 * @Author:  hunk
 */
export const saveVideoToPhone = url => {
  return new Promise((reslove, reject) => {
    if (!url) {
      uni.showToast({
        title: '视频不存在，无法下载',
        icon: 'none'
      })
      reject(false)
    } else {
      uni.showLoading({
        title: '下载中...',
        mask: true
      })
      uni.downloadFile({
        url,
        success: res => {
          const filePath = res.tempFilePath
          uni.saveVideoToPhotosAlbum({
            filePath,
            success: res => {
              reslove(true)
            },
            fail: err => {
              if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny') {
                uni.showModal({
                  title: '提示',
                  content: '需要您授权保存视频到相册',
                  success: res => {
                    if (res.confirm) {
                      uni.openSetting({
                        success: settingdata => {
                          if (settingdata.authSetting['scope.writePhotosAlbum']) {
                            uni.showModal({
                              title: '提示',
                              content: '获取权限成功,再次点击下载即可保存',
                              showCancel: false
                            })
                          } else {
                            uni.showModal({
                              title: '提示',
                              content: '获取权限失败，将无法保存到相册哦~',
                              showCancel: false
                            })
                          }
                        }
                      })
                    } else {
                      uni.showToast({
                        title: '拒绝授权',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
              reject(err)
            },
            complete: () => {
              uni.hideLoading()
            }
          })
        },
        fail: () => {
          uni.hideLoading()
          uni.showToast({
            title: '下载失败',
            icon: 'fail'
          })
        }
      })
    }
  })
}
